package com.c2.web.backend;

import com.c2.entity.EbProduct;
import com.c2.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Stars Lee
 * @date 2020/9/16 - 14:52
 */

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/list")//后台分页查询操作
    public String list(Model model,
                       @RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize",defaultValue = "4") Integer pageSize){
        System.out.println("Service Begining...");

        List<EbProduct> productList=productService.listByPage(page,pageSize);
        model.addAttribute("productList",productList);
        Integer maxPage=productService.findMaxPage(pageSize);
        model.addAttribute("maxPage",maxPage);
        Integer currentPage=page;
        model.addAttribute("currentPage",currentPage);
        return "manage/product";
    }

    @RequestMapping("/likeList")//模糊查询
    public String likeList(Model model , String inEpName,
                           @RequestParam(value = "likePage",defaultValue = "1") Integer likePage,
                           @RequestParam(value = "likePageSize",defaultValue = "2") Integer likePageSize){
        List<EbProduct> likeList=productService.likeByPage(inEpName,likePage,likePageSize);
        model.addAttribute("likeList",likeList);
        Integer likeMaxPage=productService.findLikeMaxPage(inEpName,likePageSize);
        model.addAttribute("likeMaxPage",likeMaxPage);
        Integer likeCurrentPage=likePage;
        model.addAttribute("likeCurrentPage",likeCurrentPage);
        model.addAttribute("inEpName",inEpName);
        return "manage/likeProduct";
    }

    @RequestMapping("/productView")
    public String productView(Model model,
                              @RequestParam(value = "epName")String epName,
                              @RequestParam(value = "epDescription")String epDescription,
                              @RequestParam(value = "epPrice")Double epPrice,
                              @RequestParam(value = "epStock")Integer epStock,
                              @RequestParam(value = "epFileName")String epFileName){
        model.addAttribute("epName",epName);
        model.addAttribute("epDescription",epDescription);
        model.addAttribute("epPrice",epPrice);
        model.addAttribute("epStock",epStock);
        model.addAttribute("epFileName",epFileName);
        return "manage/product-view";
    }

    @RequestMapping("/to_save")//添加页面
    public String toSave(){
        System.out.println("Service Begining...");

        return "manage/product-add";
    }
    @RequestMapping("/save_commit")//后台添加跳转判断
    public String saveCommit(EbProduct ebProduct, MultipartFile pictureFile){
        String pictureOriginalFileName=pictureFile.getOriginalFilename();
        ebProduct.setEpFileName(pictureOriginalFileName);
        File updatePicture=new java.io.File("E:\\第五学期\\课程学习\\Web实训\\easybuy-2\\src\\main\\webapp\\images\\product\\"+pictureOriginalFileName);
        try {
            if(!updatePicture.exists()){
                updatePicture.mkdirs();
            }
            pictureFile.transferTo(updatePicture);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if((ebProduct.getEpId()==0) || (ebProduct.getEpName().trim().length()==0) || (ebProduct.getEpPrice().equals(0.0)) || (ebProduct.getEpStock()==0) ||
                (ebProduct.getEpcId()==0) || ebProduct.getEpFileName().trim().length()==0 ||pictureFile.isEmpty()){
            return "manage/manage-productAddError";
        }else {
            Integer result=productService.toSaveProduct(ebProduct);
            if (result<=0){
                return "manage/manage-productAddError";
            }else {
                return "manage/manage-productAddSuccessfully";
            }
        }
    }

    @RequestMapping("/to_delete")//后台删除操作
    public String toDelete(@RequestParam(value = "epId")Integer epId){
        Integer result=productService.toDeleteProduct(epId);

        if (result<=0){
            return " ";
        }else{
            return "manage/manage-productDeleteSuccessfully";
        }
    }

    @RequestMapping("/to_update")//后台修改页面
    public String toUpdate(Model model,@RequestParam(value = "epId")Integer epId,@RequestParam(value = "currentPage")Integer currentPage){
        List<EbProduct> productUpdated=productService.selectByCondition(epId);

        model.addAttribute("productUpdated",productUpdated);
        model.addAttribute("currentPage",currentPage);
        return "manage/product-modify";
    }
    @RequestMapping("/update_submit")//修改提交跳转判断
    public String updateSubmit(Model model,EbProduct ebProduct,MultipartFile pictureFile,@RequestParam(value = "currentPage")Integer currentPage){
        String pictureOriginalFileName=pictureFile.getOriginalFilename();
        ebProduct.setEpFileName(pictureOriginalFileName);
        File updatePicture=new java.io.File("E:\\第五学期\\课程学习\\Web实训\\easybuy-2\\src\\main\\webapp\\images\\product\\"+pictureOriginalFileName);
        try {
            if(!updatePicture.exists()){
                updatePicture.mkdirs();
            }
            pictureFile.transferTo(updatePicture);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if((ebProduct.getEpId()==0) || (ebProduct.getEpName().trim().length()==0) || (ebProduct.getEpPrice().equals(0.0)) || (ebProduct.getEpStock()==0) ||
                (ebProduct.getEpcId()==0) || ebProduct.getEpFileName().trim().length()==0 || pictureFile.isEmpty()){
            model.addAttribute("epId",ebProduct.getEpId());
            return "manage/manage-productUpdateError";
        }else {
            Integer result=productService.toUpdateById(ebProduct);
            if (result<=0){
                model.addAttribute("epId",ebProduct.getEpId());
                return "manage/manage-productUpdateError";
            }else {
                model.addAttribute("currentPage",currentPage);
                return "manage/manage-productUpdateSuccessfully";
            }
        }
    }

    @RequestMapping("/addCar")
    public String addCar(HttpServletRequest request,
                         Model model,
                         @RequestParam(value = "epId",defaultValue = "201")Integer epId){
        List<EbProduct> productDetail=productService.selectByCondition(epId);

        HttpSession session=request.getSession();
        Set<EbProduct> productSet=(Set<EbProduct>) session.getAttribute("productSet");
        if (productSet==null){
            productSet=new HashSet<>(5);
        }

        for (EbProduct ebProduct :productSet) {
            if (ebProduct.getEpId().equals(productDetail.get(0).getEpId())){
                productDetail.get(0).setCount(ebProduct.getCount()+1);
                productSet.remove(ebProduct);
            }
        }
        productSet.add(productDetail.get(0));
        session.setAttribute("productSet",productSet);
//        model.addAttribute("productSet",productSet);

        return "shopping";
    }

    @RequestMapping("index")//添加/删除/修改操作成功跳转回到后台首页操作
    public String indexPage(){

        return "manage/index";
    }

}
